module ReadHelper

def link_to_issue (name,issue)
    link_to name , :controller => "read", :action => "read_issue", :day => issue.date_published.day, :year => issue.date_published.year, :month => issue.date_published.month
end

def link_to_section(name, section)
  issue = section.issue
  link_to name , :controller => "read", :action => "show_section", :day => issue.date_published.day, :year => issue.date_published.year, :month => issue.date_published.month, :name => section.name
end


def link_to_article (name, issue,section_name, article)
  
  link_to name, :controller => "read" , :action => "show_article", :day => issue.date_published.day, :year => issue.date_published.year, :month => issue.date_published.month, :name=>section_name, :id => article.id

end




# It selects an stylesheet like this: If the variable issue is not defined it
# uses the default defined in /config/cupola.yml, in case issue is set it looks
# for a custom stylesheet called volume_n_issue_n.css otherwisse it falls bac
# to the default, if the variable section is defined it looks for
# volume_n_issue_n_sectionname.css then volume_n_issue_n.css and finally falls
# back to the default

# FIXME: Rewrite this code is just ugly

 def sylesheet_selector
    default_read_styleseet = CUPOLA_CONFIG["default_read_stylesheet"]
    if !@issue.nil?
        volume = @issue.volume
        if !@section.nil?
#         Look for a custom stylesheet
          if File.exist?(RAILS_ROOT + '/public/stylesheets/' + 'volume_' + volume.volume_number.to_s + '_issue_' + @issue.issue_number.to_s+'_' + @section.name.downcase.gsub(' ', '_') + '.css')
             stylesheet_link_tag 'volume_' + volume.volume_number.to_s + '_issue_' + @issue.issue_number.to_s + '_' + @section.name.downcase.gsub(' ', '_') + '.css' , :media => "all"

          elsif File.exist?(RAILS_ROOT+'/public/stylesheets/'+'volume_'+volume.volume_number.to_s+'_issue_'+@issue.issue_number.to_s+'.css')
              stylesheet_link_tag 'volume_'+volume.volume_number.to_s+'_issue_'+@issue.issue_number.to_s+'.css' , :media => "all"
          else
            stylesheet_link_tag default_read_styleseet, :media => "all"
          end
#         If there is not a section defined
        else
#           Try to find a custom stylesheet
          if  File.exist?(RAILS_ROOT+'/public/stylesheets/'+'volume_'+volume.volume_number.to_s+'_issue_'+@issue.issue_number.to_s+'.css')
              stylesheet_link_tag 'volume_'+volume.volume_number.to_s+'_issue_'+@issue.issue_number.to_s+'.css' , :media => "all"
          else
            stylesheet_link_tag default_read_styleseet, :media => "all"
          end
        end
#       If there is not an issue defined..., like in a search or archive
      else
        stylesheet_link_tag default_read_styleseet, :media => "all"
      end

  end


end


#       if !@section.nil?
#
#         if File.exist?(RAILS_ROOT+'/public/stylesheets/' + @issue.issue_number.to_s + @section.name.downcase + '.css')
#
#           stylesheet_link_tag @issue.issue_number.to_s + @section.name.downcase, :media => "all"
#
#         elif File.exist?(RAILS_ROOT+'/public/stylesheets/' +  @section.name.downcase + '.css')
#
#           stylesheet_link_tag  @section.name.downcase, :media => "all"
#
#
#         end
#
#       else
#         stylesheet_link_tag "read", :media => "all"
#
#       end
#
#       if File.exist?(RAILS_ROOT+'/public/stylesheets/issue_' + @issue.issue_number.to_s + '.css')
#
#         stylesheet_link_tag 'issue_'+@issue.issue_number.to_s, :media => "all"